package ru.ifmo.swing;

import org.springframework.beans.factory.annotation.Required;
import org.uncommons.watchmaker.swing.AbortControl;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author avhaliullin
 */
public class RunButton extends JButton {
    private Runnable task;
    private AbortControl abortControl;
    private List<ProgressStartListener> progressStartListeners;

    {
        setText("Run");
        addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doRun();
            }
        });
    }

    public void doRun() {
        if (abortControl != null) {
            abortControl.reset();
        }
        if (progressStartListeners != null) {
            for (ProgressStartListener listener : progressStartListeners) {
                listener.progressStarted();
            }
        }
        new Thread(task).start();
    }

    @Required
    public void setTask(Runnable task) {
        this.task = task;
    }

    public void setAbortControl(AbortControl abortControl) {
        this.abortControl = abortControl;
    }

    public void setProgressStartListenersList(List<ProgressStartListener> progressStartListener) {
        this.progressStartListeners = progressStartListener;
    }
}
